﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SE560Multimedia.src;

namespace SE560Multimedia.pages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private SessionManager sessionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionManager = new SessionManager(Session);

            ProjectDataSetTableAdapters.mediaTableAdapter mediaTableAdapter =
                new ProjectDataSetTableAdapters.mediaTableAdapter();

            ProjectDataSet.mediaDataTable playlistResult =
                mediaTableAdapter.GetDataByListId(sessionManager.SessionLoggedPlaylistID);

            sessionManager.PlaylistResult = playlistResult;

            GridViewPlaylist.DataSource = playlistResult;
            GridViewPlaylist.DataBind();

            String operationResultText;
            if (playlistResult.Rows.Count > 0)
            {
                operationResultText = "User Play List";
            }
            else
            {
                operationResultText = "There is no play list item";
            }
            LabelPlayListStatus.Text = operationResultText;
        }

        protected void GridViewPlaylist_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowId = Convert.ToInt32(e.CommandArgument);

            ProjectDataSet.mediaRow selectedMediaRow = 
                sessionManager.PlaylistResult[rowId];

            if (e.CommandName == "ResultListPlayCommand")
            {
                sessionManager.CurrentDataRow = selectedMediaRow;
                System.Web.HttpContext.Current.Response.Redirect(SessionManager.PLAY_PAGE_ADDESS);

            }
            else if (e.CommandName == "ResultListDeleteCommand")
            {
                int mediaId = (int)selectedMediaRow["MEDIA_ID"];

                ProjectDataSetTableAdapters.playlistitemTableAdapter plyLstItemAdapter = new
                    ProjectDataSetTableAdapters.playlistitemTableAdapter();
                plyLstItemAdapter.DeleteQueryByMediaId(mediaId);

                // Refresh page for updating result
                Page.Response.Redirect(Page.Request.Url.ToString(), true);
            }
        }
    }
}
